<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.js"></script>
</head>

<body>



<script>
    mounted ();
    function mounted() {
        var host = document.location.host;
        const url = 'ws://127.0.0.1:8082/' + 'webSocket/' + "user";
        var websocket = null
        // alert(url)
        if ('WebSocket' in window) {
            websocket = new WebSocket(url);
            //alert("支持WebSocket协议");
        } else {
            alert("不支持WebSocket协议");
        }

        //WebSocket连接发生错误的回调方法
        websocket.onerror = function () {
            console.log("连接失败")
        };

        //WebSocket连接成功建立的回调方法
        websocket.onopen = function () {
            console.log("WebSocket连接成功");
        };

        //接收到消息的回调方法
        websocket.onmessage = (event)=> {
            console.log("接收到推送过来的信息",event.data);
            document.write("接收到MQ消息为："+event.data);

            // this.$notify({
            //     showClose: true,
            //     message: '单击'+JSON.stringify(event.data),
            //     type: 'success',
            // });
            // websocket.close();
            // alert("webSocket已关闭！")
        };

        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function () {
            //closeWebSocket();
        };

        //关闭WebSocket连接
        function closeWebSocket() {
            websocket.close();
        }

        //连接关闭的回调方法
        websocket.onclose = function () {
            console.log("WebSocket连接关闭");
        };}


</script>

</body>
</html>